package com.budwk.app.sqfwV2.generator.meeting;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfw.enums.DownLoadFileNameEnum;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfwDoc.bo.SqfwExcelForSummaryBO;
import com.budwk.app.sqfwDoc.bo.SqfwExcelJudgeRes;
import com.budwk.app.sqfwDoc.codeGenerator.cusType.SqfwImage;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.BaseTemplateExcelGenerator;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.SqfwExcelCreateCommonBO;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.AbstractCustomerPdfCreateField;
import com.budwk.app.sqfwV2.dto.MeetingExcelImageConfigBO;
import com.budwk.app.sqfwV2.dto.MeetingParamDTO;
import com.budwk.app.sqfwV2.enums.MeetingEnums;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.sys.services.SysUserService;
import com.budwk.app.util.poiUtils.excelPictureConfigBO.ExcelFileCreateParamBO;
import com.google.common.collect.Lists;
import org.nutz.ioc.loader.annotation.Inject;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.budwk.app.util.poiUtils.CommonFormOfficeUtils.addPictureToByteList;

public class MeetingExcelGenerator extends BaseTemplateExcelGenerator {


    private String templatePath;

    @Override
    protected String getTemplateName() {
        return templatePath;
    }

    public MeetingExcelGenerator(String templatePath) {
        this.templatePath = templatePath;
    }


    @Override
    public String getDownloadFileName() {
        MeetingEnums meetingEnums = MeetingEnums.getTemplatEnums(templatePath);
        String downLoadFile = null;
        if (null != meetingEnums) {
            if (MeetingEnums.MEETING_MINUTES_EXCEL.getCode() == meetingEnums.getCode()) {
                downLoadFile = DownLoadFileNameEnum.MEETING_MINUTES_EXCEL.getName();
            } else if (MeetingEnums.MEETING_MINUTES_DETAIL_EXCEL.getCode() == meetingEnums.getCode()) {
                downLoadFile = DownLoadFileNameEnum.MEETING_MINUTES_DETAIL_EXCEL.getName();
            } else if (MeetingEnums.VISITING_RECORDS_EXCEL.getCode() == meetingEnums.getCode()) {
                downLoadFile = DownLoadFileNameEnum.MEETING_VISITING_RECORDS_EXCEL.getName();
            } else if (MeetingEnums.ACTIVITIES_EXCEL.getCode() == meetingEnums.getCode()) {
                downLoadFile = DownLoadFileNameEnum.MEETING_ACTIVITIES_EXCEL.getName();
            }
        }
        return downLoadFile +"&"+ DateUtil.format(new Date(), "yyyyMMddHHmmss");
    }

    /**
     * 可以参照 {@link com.budwk.app.sqfw.enums.DownLoadFileNameEnum}
     *
     * @return 文件名名称
     */


    /**
     * 用户自定义excel基础参数，比如表头这样的公用数据
     *
     * @param excelParam       已经填充的基础数据据
     * @param excelCreateParam excel生成提供的原料
     */
    @Override
    protected void customerPopularSheetBaseParam(Map<String, Object> excelParam, SqfwExcelCreateCommonBO excelCreateParam) {

    }

    @Override
    protected void customerPopularSheetDataParam(List<Map<String, Object>> listMap, SqfwExcelCreateCommonBO excelCreateParam) {

    }

    @Override
    protected void completeExcelFileCreateParam(ExcelFileCreateParamBO excelFileCreateParam) {

    }

    @Override
    protected void customerPopularRowDataMap(Map<String, Object> rowDataMap, SqfwExcelForSummaryBO forSummaryBO) {
        Sqfw_formSummary_new sqfwFormSummaryNew = forSummaryBO.getSqfwFormSummaryNew();
        rowDataMap.put("fillDateStr",DateUtil.format(sqfwFormSummaryNew.getFillDate(),"yyyy-MM-dd HH:mm"));
        rowDataMap.put("community", sqfwFormSummaryNew.getCommunity());
        rowDataMap.put("county", sqfwFormSummaryNew.getCounty());
    }

    @Override
    protected SqfwExcelJudgeRes judgeDataIntoExcelBeforeErgodic(SqfwExcelCreateCommonBO excelCreateParam, Map<String, Object> excelParam) {
        return null;
    }

    @Override
    protected SqfwExcelJudgeRes judgeDataIntoExcelBeforeCreate(Map<String, Object> excelParam) {
        return null;
    }


    @Override
    protected Class<?> getImageConfigBO() {
        return MeetingExcelImageConfigBO.class;
    }

    protected Object getFieldValueByType(Field declaredField, AbstractCustomerPdfCreateField fieldData) {

        Object fieldValue = null;

        try {
            fieldValue = declaredField.get(fieldData);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (fieldData == null) {
            return "";
        }

        // 分不同的类型进行处理，如果是字符串的话，可以直接插入数据
        if (String.class.isAssignableFrom(declaredField.getType())) {
            return fieldValue;
        }

        // 如果是时间类型，则使用默认的时间格式进行转化
        if (Date.class.isAssignableFrom(declaredField.getType())) {
            Date fileDate = (Date) fieldValue;

            return DateUtil.format(fileDate, "yyyy-MM-dd");
        }

        // 如果是照片类型则转化为图片
        if (SqfwImage.class.isAssignableFrom(declaredField.getType())) {
            SqfwImage imageData = (SqfwImage) fieldValue;

            List<byte[]> imageByteList = Lists.newArrayList();
            if (imageData == null) return imageByteList;

            List<String> pictureUrlList = imageData.getImageUrlList();
            if (pictureUrlList == null || pictureUrlList.size() == 0) return imageByteList;

            if (pictureUrlList.size() > 2) pictureUrlList = pictureUrlList.subList(0, 3);
            imageByteList = addPictureToByteList(pictureUrlList);

            return imageByteList;
        }

        return fieldValue;
    }
}
